<?php

require_once "../../php/config/config.php";
require_once "../model/adminDb.php";
$AdminDb = new AdminDb();
$name = $_SESSION['name'];
$url = $_SERVER["QUERY_STRING"];
$id = str_replace("id=", '', $url);
$dat = $AdminDb->query_userName($name);
$card = $dat[0]['card'];
$userId = $AdminDb->queryBug_id($id);
$newName = $userId[0]['newName'];
$status = $userId[0]['status'];

?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="../../static/js/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/css/view.css"/>
    <title></title>
    <style>
        #test10 {
            margin-left: 100px;
        }

        .imgCss {
            margin-left: 20px;
            height: 200px;
            cursor: pointer;
        }

        #status_title {
            font-size: 28px;
        }

        .status {
            width: 30%;

            text-align: center;
            /*background-color: green;*/
        }

        #status_div {
            background-color: #F0F2F5;
            display: flex;
            color: white;
            flex-direction: row;
        }

        #status_box {
            display: flex;
            flex-direction: row;
            width: 100%;

        }


    </style>
</head>
<body class="layui-view-body">
<div class="layui-content">
    <div class="layui-row">
        <div class="layui-card">
            <div class="layui-card-header">编辑Bug</div>
            <form class="layui-form layui-card-body" action="" lay-filter="example">
                <div class="layui-form-item">
                    <label class="layui-form-label">产品</label>
                    <div class="layui-input-block">
                        <select name="product" id="product" lay-filter="product">
                            <option value=""></option>

                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">平台</label>
                    <div class="layui-input-block" lay-filter="pingtai" id="pingtai">

                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">负责人</label>
                    <div class="layui-input-block">
                        <select name="person" id="person" lay-filter="person">
                            <option value=""></option>

                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" required lay-verify="title" placeholder="请输入标题"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">正文</label>
                    <div class="layui-input-block">
                        <script id="container" name="content" type="text/plain">
                   <h1></h1>
                        </script>
                    </div>
                </div>
                <br>
                <div class="layui-form-item" style="margin-top: 20px">
                    <div class="layui-inline">
                        <label class="layui-form-label">新建人</label>
                        <div class="layui-input-inline">
                            <input name="new_name" class="layui-input" type="text" disabled autocomplete="off" value="<?php echo $name ?>" lay-verify="new_name">
                        </div>
                    </div>
                </div>
                <br>
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                    <legend>状态</legend>
                </fieldset>
                <div id="status_div">
                    <div class="status" id="one"><span>测试完成 30%</span></div>
                    <div class="status" id="two"><span>修改完成 60%</span></div>
                    <div class="status" id="three"><span>完成100%</span></div>
                </div>
                <div id="statusBox">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                        <legend>编辑状态</legend>
                    </fieldset>
                    <div class="layui-upload layui-row" id="status_box">
                        <?php
                        if ($status == "0") {
                            echo "  <a id='status0' class=\"layui-btn  layui-btn-danger layui-col-xs2\" onclick=\"editStatus(3)\" lay-submit lay-filter=\"editStatus\">待完成</a>";
                            echo " <a class=\"layui-btn layui-btn-primary layui-col-xs2 \" id=\"status2\" onclick=\"editStatus(1)\" lay-submit lay-filter=\"editStatus\">完成解决</a>";
                        } elseif ($status == "1") {
                            if ($card == 1 || $name == $newName) {
                                echo " <a class=\"layui-btn layui-btn-primary layui-col-xs2 \" id=\"status2\" onclick=\"editStatus(2)\" lay-submit lay-filter=\"editStatus\">完成解决</a>";
                                echo "  <a id='status0' class=\"layui-btn layui-btn-primary layui-col-xs2\" onclick=\"editStatus(0)\" lay-submit lay-filter=\"editStatus\">驳回</a>";
                            }
                        } elseif ($status == "2") {
                            if ($card == 1 || $name == $newName) {
                                echo "  <a id='status0' class=\"layui-btn layui-btn-primary layui-col-xs2\" onclick=\"editStatus(0)\" lay-submit lay-filter=\"editStatus\">驳回</a>";
                            }
                        } elseif ($status == "3") {
                            echo " <a class=\"layui-btn layui-btn-primary layui-col-xs2 \" id=\"status2\" onclick=\"editStatus(1)\" lay-submit lay-filter=\"editStatus\">完成解决</a>";
                        }
                        ?>
                    </div>
                </div>
                <div id="editBtn">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                        <legend>确认修改</legend>
                    </fieldset>
                    <div class="layui-upload layui-row" id="status_box">
                        <a class="layui-btn  layui-col-xs2 " id="status2" lay-submit lay-filter="editBut">保存</a>

                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 配置文件 -->
<script type="text/javascript" src="../../static/uedit/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="../../static/uedit/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var ue = UE.getEditor('container', {
        toolbars: [
            ['snapscreen', 'fullscreen', 'undo', 'redo', 'bold', 'underline', 'insertvideo', 'forecolor', 'scrawl', 'template', 'emotion']
        ],
        initialFrameHeight: 240,
        autoHeightEnabled: true,
        autoFloatEnabled: true,
    });
</script>
<script type="text/javascript" src="../../static/js/jquery-3.3.1.min.js"></script>
<script src="../../static/js/layui/layui.all.js"></script>

<script>
    var regex2 = /\"(.+?)\"/g; // [] 中括号
    var img_arr = new Array();
    var img_arr1 = new Array();
    var url = document.location.toString();
    var id = url.split("=")[1];
    query(id);
    $("#test2").click(function () {
        $("#imgDiv").children().remove();
    });

    //获取bug数据函数
    function query(id) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/query_bug.php',
            data: {
                "id": id,
            },
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {//获取成功
                    assignment(res.data);

                }
            },
            error: function (res) {
                layer.msg("连接失败！");
            }


        });
    }

    function assignment(value) {
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            //数据提取
            $product = value[0].product;
            $person = value[0].person;
            $title = value[0].title;
            $newName = value[0].newName;
            $desc = value[0].desction;
            $imgData = value[0].image;
            $content = value[0].content;
            $imgData = $imgData.replace("[", "").replace("]", "");
            $imgData1 = $imgData.split(",");
            img_arr.push($imgData);
            //显示图片
            for ($i = 0; $i < $imgData1.length; $i++) {
                $imgUrl = $imgData1[$i].replace("\"", "").replace("\"", "").split("php/")[1];
                $domain = window.location.href.split("view")[0];
                if ($imgUrl == undefined) {
                    $li = $("<span>暂无！</span>");
                    $("#imgDiv").append($li);
                }
                else {
                    $imgUrl = $domain + $imgUrl;
                    $imgId = "img" + $i;
                    $li = $("<img class='imgCss' id=\"" + $imgId + "\" onclick='show_img(\"" + $i + "\",\"" + $imgUrl + "\")' src=\"" + $imgUrl + "\" />");
                    $("#imgDiv").append($li);
                }

            }
            //显示状态
            $status = value[0].status;
            if ($status == "0") {//未完成状态
                $("#status_title").css({"color": "red"});
                $("#one").css({"background": "#009688"});
            }
            else if ($status == "1") {
                $("#status_title").css({"color": "white"});
                $("#one").css({"background": "#009688"});
                $("#two").css({"background": "#009688"});
            }
            else if ($status == "2") {
                $("#status_div").css({"background-color": "#009688"});
                $("#status_title").css({"color": "white"});
                $("#status2").css({"display": "none"});
            }
            else if ($status == "3") {
                $("#one").css({"background": "#FF5722"});
                $("#two").css({"background": "#FF5722"});
                $("#two").html("待完成");
            }
            selectProject($product);


            //表单初始赋值
            form.val('example', {
                "product": $product,
                "person": $person,
                "title": $title,
                "desc": $desc,
                "new_name": $newName,
            });

            ue.ready(function () {
                ue.setContent($content, false);
            });
            //负责人赋值
            selectPerson($person);
            //平台赋值
            $platfrom = value[0].platfrom;
            $platfrom = $platfrom.split(",");
            $platArray = new Array('android', 'ios', 'PC', '安卓平板', '苹果平板');

            for ($j = 0; $j < $platArray.length; $j++) {
                $platfromName = $platArray[$j];
                $li = $(" <input type=\"checkbox\" name=\"platfrom\"  value=\"" + $platfromName + "\" title=\"" + $platfromName + "\" >");
                for ($i = 0; $i < $platfrom.length; $i++) {
                    if ($platfromName == $platfrom[$i]) {
                        $li = $(" <input type=\"checkbox\" name=\"platfrom\" checked value=\"" + $platfrom[$i] + "\" title=\"" + $platfrom[$i] + "\" >");
                        $("#pingtai").append($li);
                    }

                }
                $("#pingtai").append($li);

            }

        });
    }

    //查看图片
    function show_img(id, url) {
        $id = "#img" + id;
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: ['90%', '90%'],
            shadeClose: true,
            skin: 'yourclass',
            content: '<img src=\'' + url + '\' style="width: 100%" alt="图片预览" />'
        });
    }

    //负责人选择框
    function selectPerson(person) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/query_user.php',
            data: {},
            dataType: 'json',
            success: function (res) {


                $dat = res.data;
                $count = $dat.length;
                var select_person = document.getElementById("person");
                for ($i = 0; $i < $count; $i++) {
                    if ($dat[$i].name == person) {
                        select_person.innerHTML += "<option value='" + $dat[$i].name + "'  selected=\"\">" + $dat[$i].name + "</option>";
                    }
                    else {
                        select_person.innerHTML += "<option value='" + $dat[$i].name + "' >" + $dat[$i].name + "</option>";
                    }


                }
                layui.use('form', function () {
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                    form.render('select', 'select_person'); //刷新select选择框渲染
                });


            },
            error: function (res) {
                console.log("加载失败");
            }


        });


    }

    //图片上传
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //多图片上传
        upload.render({
            elem: '#test2'
            , url: '../controller/upload.php'
            , multiple: true
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#imgDiv').append('<img  src="' + result + '" alt="' + file.name + '" class="imgCss">')
                });
            }
            , done: function (res) {
                layer.msg(res.msg);
                //上传成功
                if (res.code == "200") {
                    img_arr1.push(res.data.src);
                }
                //上传失败
                else {
                    $("#imgDiv").children().remove();
                }
            }
        });
    });
    layui.use('form', function () {
        var form = layui.form;
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 1) {
                    return '标题太短';
                }
            },
        });
        //监听提交
        form.on('submit(editBut)', function (data) {
            $product = $("#product").val();
            $interest = $("#interest").val();

            if ($product == "") {
                layer.msg("未选择产品");
                return false;

            }
            text = $("input:checkbox[name='platfrom']:checked").map(function (index, elem) {
                return $(elem).val();
            }).get().join(',');
            if (text == "") {
                layer.msg("未选择平台");
                return false;
            }
            if ($interest == "") {
                layer.msg("未选择负责人");
                return false;
            }
            var data = data.field;
            img_arr1 = JSON.stringify(img_arr1);
            img_arr = JSON.stringify(img_arr);
            if (img_arr1.length == 2) {
                img_arr1 = img_arr;
            }
            $.ajax({
                type: 'post',
                cache: 'false',
                url: '../controller/edit_bug.php',
                data: {
                    "data": data,
                    "platfrom": text,
                    "imgUrl": img_arr1,
                    "id": id
                },
                dataType: 'json',
                success: function (res) {
                    layer.msg(res.message);
                    // if (res.code == 200) {
                    //     window.location.href = "../../index.php";
                    //
                    // }


                },
                error: function (dat) {
                    layer.msg("保存失败");
                    return false;

                }


            });
        });
    });

    //完成解决
    function editStatus(val) {
        $.ajax(
            {
                type: 'post',
                cache: 'false',
                url: '../controller/edit_status.php',
                data: {
                    "id": id,
                    "val": val,
                },
                dataType: 'json',
                success: function (res) {
                    layer.msg(res.message);
                    if (res.code == 200) {
                        window.location.reload();
                    }
                },
                error: function (res) {
                    layer.msg("更新失败");
                }
            }
        )


    }

    //项目选择
    function selectProject(val) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/project.php',
            data: {},
            dataType: 'json',
            success: function (res) {
                $dat = res.data;
                $count = $dat.length;
                var product = document.getElementById("product");
                for ($i = 0; $i < $count; $i++) {

                    if ($dat[$i].name == val) {
                        product.innerHTML += "<option value='" + $dat[$i].name + "'  selected=\"\" >" + $dat[$i].name + "</option>";
                    }
                    else {
                        product.innerHTML += "<option value='" + $dat[$i].name + "' >" + $dat[$i].name + "</option>";
                    }


                }
                layui.use('form', function () {
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                    form.render('select', 'select_person'); //刷新select选择框渲染
                });


            },
            error: function (res) {
                console.log("加载失败");
            }


        });


    }
</script>
</body>
</html>